package com.loukou.lkt.web.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alex.csvhelper.CsvWriteException;
import com.alex.csvhelper.CsvWriter;
import com.loukou.lkt.lkt.bo.PageList;
import com.loukou.lkt.lkt.bo.SupplierBo;
import com.loukou.lkt.lkt.bo.SupplierExportCsvBean;
import com.loukou.lkt.lkt.enums.InvoiceTypeEnum;
import com.loukou.lkt.lkt.enums.SettlementTypeEnum;
import com.loukou.lkt.lkt.enums.SupplierIsExamineEnum;
import com.loukou.lkt.lkt.enums.SupplierTypeEnum;
import com.loukou.lkt.lkt.resp.Column;
import com.loukou.lkt.lkt.resp.RespDto;
import com.loukou.lkt.lkt.service.SupplierService;
import com.loukou.lkt.lkt.utils.FileUtil;

@Controller
// @RequestMapping("/supplier")
public class SupplierController extends BaseController {

	@Autowired
	private SupplierService supplierService;

	@RequestMapping(value = "/api/supplier/getListData", method = RequestMethod.GET)
	@ResponseBody
	public RespDto<PageList<SupplierBo>> getListData(
			@RequestParam(value = "supplierCode", required = false, defaultValue = "") String supplierCode,
			@RequestParam(value = "supplierName", required = false, defaultValue = "") String supplierName,
			@RequestParam(value = "settmentType", required = false, defaultValue = "-1") int settmentType,
			@RequestParam(value = "isExamine", required = false, defaultValue = "-1") int isExamine,
			@RequestParam(value = "isUse", required = false, defaultValue = "-1") int isUse,
			@RequestParam(value = "pageIndex", required = false, defaultValue = "1") int pageIndex,
			@RequestParam(value = "pageNum", required = false, defaultValue = "10") int pageNum) {
		RespDto<PageList<SupplierBo>> dto = supplierService.getList(supplierCode, supplierName, settmentType, isExamine,
				isUse, pageIndex, pageNum);
		return dto;
	}

	@RequestMapping(value = "/api/supplier/settlementTypes", method = RequestMethod.GET)
	@ResponseBody
	public List<Column<Integer, String>> getsettlementTypes() {
		List<Column<Integer, String>> cls = new ArrayList<Column<Integer, String>>();
		for (SettlementTypeEnum typeEnum : SettlementTypeEnum.values()) {
			Column<Integer, String> cl = new Column<Integer, String>(typeEnum.getId(), typeEnum.getName());
			cls.add(cl);
		}
		return cls;
	}

	@RequestMapping(value = "/api/supplier/supplierState", method = RequestMethod.GET)
	@ResponseBody
	public List<Column<Integer, String>> getSupplierState() {
		List<Column<Integer, String>> cls = new ArrayList<Column<Integer, String>>();
		for (SupplierIsExamineEnum typeEnum : SupplierIsExamineEnum.values()) {
			Column<Integer, String> cl = new Column<Integer, String>(typeEnum.getType(), typeEnum.getComment());
			cls.add(cl);
		}
		return cls;
	}

	@RequestMapping(value = "/api/supplier/supplierTypes", method = RequestMethod.GET)
	@ResponseBody
	public List<Column<Integer, String>> getSupplierTypes() {
		List<Column<Integer, String>> cls = new ArrayList<Column<Integer, String>>();
		for (SupplierTypeEnum typeEnum : SupplierTypeEnum.values()) {
			Column<Integer, String> cl = new Column<Integer, String>(typeEnum.getId(), typeEnum.getName());
			cls.add(cl);
		}
		return cls;
	}

	@RequestMapping(value = "/api/supplier/invoiceTypes", method = RequestMethod.GET)
	@ResponseBody
	public List<Column<Integer, String>> getInvoiceTypes() {
		List<Column<Integer, String>> cls = new ArrayList<Column<Integer, String>>();
		for (InvoiceTypeEnum typeEnum : InvoiceTypeEnum.values()) {
			Column<Integer, String> cl = new Column<Integer, String>(typeEnum.getId(), typeEnum.getName());
			cls.add(cl);
		}
		return cls;
	}

	@RequestMapping(value = "/api/supplier/save", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public RespDto<SupplierBo> save(@RequestBody SupplierBo supplierBo) {
		//supplierBo.setCreateBy();
		return supplierService.save(supplierBo);
	}

	@RequestMapping("/web/supplier/index")
	public String index() {
		return "supplier/index";
	}

	@RequestMapping("/web/supplier/edit")
	public String edit() {
		return "supplier/edit";
	}

	@RequestMapping("/web/supplier/detail")
	public String detail() {
		return "supplier/detail";
	}

	@RequestMapping(value = "/api/supplier/detail", method = RequestMethod.GET)
	@ResponseBody
	public RespDto<SupplierBo> getDetail(
			@RequestParam(value = "orderId", required = false, defaultValue = "-1") int orderId) {
		RespDto<SupplierBo> resp = new RespDto<SupplierBo>();
		resp.setResult(supplierService.getDetail(orderId));
		return resp;
	}

	@RequestMapping(value = "/api/supplier/updateIsUse", method = RequestMethod.GET)
	@ResponseBody
	public RespDto<Integer> updateIsUse(@RequestParam(value = "id", required = false, defaultValue = "-1") int Id,
			@RequestParam(value = "isUse", required = false, defaultValue = "0") int IsUse) {
		return supplierService.updateIsUse(Id, IsUse);
	}

	// 导出
	@RequestMapping(value = "/api/supplier/exportCsv")
	@ResponseBody
	public void exportCsv(
			@RequestParam(value = "supplierCode", required = false, defaultValue = "") String supplierCode,
			@RequestParam(value = "supplierName", required = false, defaultValue = "") String supplierName,
			@RequestParam(value = "settmentType", required = false, defaultValue = "-1") int settmentType,
			@RequestParam(value = "isUse", required = false, defaultValue = "-1") int isUse,
			HttpServletResponse response) {
		String downloadFilePath = FileUtil.getDownloadFilePath();
		String filePath = downloadFilePath + "supplierExport_" + (new Date().getTime()) + ".csv";
		List<SupplierExportCsvBean> supplierList = supplierService.exportSupplierList(supplierCode, supplierName,
				settmentType, isUse);
		CsvWriter<SupplierExportCsvBean> writer = new CsvWriter<SupplierExportCsvBean>(filePath,
				SupplierExportCsvBean.class);
		try {
			writer.start();
			if (supplierList != null && supplierList.size() > 0) {
				try {
					
					writer.append(supplierList);
				} catch (CsvWriteException e) {
					e.printStackTrace();
				}
			}
			writer.end();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		response.setContentType("application/csv");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-disposition", "attachment; filename=supplierExport.csv");
		ServletOutputStream os;
		try {
			os = response.getOutputStream();
			File file = new File(filePath);
			InputStream is = new FileInputStream(file);
			byte[] byteArr = new byte[1024];
			byte[] bomStrByteArr = new byte[] { (byte) 0xef, (byte) 0xbb, (byte) 0xbf };
			os.write(bomStrByteArr);
			while (is.read(byteArr) > 0) {
				
				os.write(byteArr);
				byteArr = new byte[1024];
			}
			is.close();
			file.delete();
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
